package operaciones;


import fuentes.Fuente;
import fuentes.Mensajes;
import fuentes.Normal;
import fuentes.Token;


public abstract class OperacionAbstracta {

	protected Fuente fuente;
	protected Mensajes m;
	protected String buffer;
	public OperacionAbstracta(String buffer,Fuente fuente, Mensajes m) 
	{
		super();
		
		this.fuente = fuente;
		this.m = m;
		this.buffer = buffer;
	}
	
	public abstract Token ejecutar(char a, long nro_linea);
	
	public void setBuffer(String b)
	{
		buffer=b;
	}
	
	public String getBuffer(){
		return buffer;
	}
	
	protected Normal corrimiento(String a){
		if (Double.parseDouble(a)==0) return new Normal(new String("0.0"),-1);
		if (!a.contains(".")) a=a+".0";
		if(a.charAt(0)=='.') {
			
			a="0"+a;
		}
		if(a.endsWith(".")) a=a+"0";
		int i;
		int mov=0;
		for(i=0;(a.charAt(i)!='.');i++){}
		String last=a.substring(i+1, a.length());
		String first=a.substring(0, i);
		if (first.length()>1)
			while ((first.length()>1)&&(first.charAt(0)=='0'))
				first=first.substring(1,first.length());
		if (first.matches("0")){
			while(first.matches("0")){
				first=last.substring(0, 1);
				last=last.substring(1, last.length());
				if (last.matches("")) last="0";
				mov--;
	
			}
		}else
		{	
			while(first.length()!=1){
				last=first.substring(first.length()-1, first.length())+last;
				first=first.substring(0, first.length()-1);
				mov++;
			
			}
			
		}
		if (last.length()>1)
			while ((last.length()>1)&&(last.endsWith("0"))){
				last=last.substring(0,last.length()-1);
			}		
		return new Normal(new String(first+"."+last),mov);
	}
	
	
	protected String normalizar(String a){
		if ((!a.contains("e"))&&(!a.contains("."))){
			if (a.length()>1)
				while ((a.length()>1)&&(a.startsWith("0")))
					a=a.substring(1,a.length());
			return a;
		}else{
		if (!a.contains("e")){
			Normal cor=corrimiento(a);
			return new String(cor.getMantiza()+"e"+cor.getExponente());
		}else
		{
			int i;
			for(i=0;(a.charAt(i)!='e');i++){}
			String last=a.substring(i+1, a.length());
			if (last.contains("+"))
				last=(String) last.subSequence(1, last.length());
			long lastL=Long.parseLong(last);
			String first=a.substring(0, i);
			Normal cor=corrimiento(first);
			double firstD=Double.parseDouble(first);
			if (firstD==0)
				return (cor.getMantiza()+"e0");
			else{
				lastL=lastL+cor.getExponente();
				return (cor.getMantiza()+"e"+lastL);
			}
			
				
		}
		}

	}
	
	protected boolean rango(String a){
		//CAMBIAR RANGO POR EL QUE NOS PIDEN
		if ((!a.contains("e"))&&(!a.contains("E"))){
			long lastL=Long.parseLong(a);
			if ((lastL)>(Math.pow(2,32)-1))
				return false;
			return true;
		}else
		{
			int i;
			for(i=0;(a.charAt(i)!='e');i++){}
			String last=a.substring(i+1, a.length());
			float lastD=Float.parseFloat(last);
			String first=a.substring(0, i);
			float firstD=Float.parseFloat(first);
			float nro=(float) (firstD*Math.pow(10,lastD));
			float max=(float) (3.40282347*Math.pow(10,38));
			float min=(float) (1.17549435*Math.pow(10,-38));
			if ((nro>max)||(nro<min)) return false;
			return true;
			
		}
	}
}
